package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: Loser
 * @Date: Created in 2020-10-15 13:28
 * @Description: 菜单业务层实现类
 * @Path: com.itheima.service.MenuServiceImpl
 * @Modified By:
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    /**
     * 添加菜单项
     * @param menu
     */
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    /**
     * 分页查询
     * @param queryPageBean
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.findPage(queryString);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
     * 修改菜单项
     * @param menu
     */
    @Override
    public void edit(Menu menu) {
        menuDao.update(menu);
    }

    /**
     * 根据id进行查询，数据回显
     * @param id
     * @return
     */
    @Override
    public Menu findById(Integer id) {
        return menuDao.findByMenuId(id);
    }

    /**
     * 删除菜单项
     * @param id
     */
    @Override
    public void delete(Integer id) {
        menuDao.delete(id);
    }

    /**
     * 查询父菜单
     * @return
     */
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }
}
